import Vue from 'vue'
import VueRouter from 'vue-router'
import * as authUtils from '@/utils/authUtils.js';
const settings = require('@/settings.js');
import Layout from '@/components/Layout/index.vue';
import MenuRouteView from '@/components/MenuRouteView/index.vue';
import Page404 from '@/views/error/Page404.vue';

Vue.use(VueRouter);

/**
 * 静态路由
 */
const constRoutes = [
    {
        path: '/',
        name: 'Index',
        component: Layout,
        meta: {
            title: '首页',
        },
        children: [
            {
                path: '',
                component: () => import('@/views/admin/index.vue'),
                meta: {
                    title: '首页',
                },
            },
            {
                path: '/apoUser',
                component: () => import('@/views/appUser/List.vue'),
                meta: {
                    title: '用户管理',
                },
            },
            {
                path: '/codePlace',
                component: () => import('@/views/codePlace/List.vue'),
                meta: {
                    title: '用户管理',
                },
            },
        ]
    },
    // 数据展示屏
    {
        path: '/dataView',
        name: 'DataView',
        component: () => import('@/views/dataView/index.vue'),
        meta: {
            title: '大数据平台',
            // 不需要登录
            needLogin: false,
        },
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue'),
        meta: {
            title: '登录',
            // 不需要登录
            needLogin: false,
        },
    },
    {
        path: '/*',
        component: Page404,
        meta: {
            title: '404',
            needLogin: false,
        },
    },
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes: constRoutes
});

/**
 * 进入下一个路由之前执行
 */
router.beforeEach((to, from, next)=>{
    console.log("进入下一个路由之前执行 to: ", to);
    // 默认所有页面都需要登录，不需要登录则在meta参数中指定。
    let needLogin = to.meta.needLogin;
    if (needLogin === false){
        // 不需要登录
        return next();
    }

    if (authUtils.hasLogin()) {
        return next();
    }

    // 重定向到登录页面
    return next({
        name: "Login",
        // 从哪个路径进入的登录页面
        redirectPath: to.fullPath,
    });
});

/**
 * 进入路由之后
 */
router.afterEach((to, from) =>{
    // 1. 设置浏览器标签标题
    // 路由标题
    let routeTitle = to.meta.title;
    let title = routeTitle ? routeTitle + "-" + settings.title : settings.title;
    document.title = title;
});


// 解决报错
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err);
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
    return originalReplace.call(this, location).catch(err => err);
}


export default router
